package com.yl.service.impl;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.gridfs.GridFSFindIterable;
import com.yl.config.MongoConfig;
import com.yl.entity.Inventory;
import com.yl.query.InventoryQuery;
import com.yl.service.InventoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangl
 */
@DubboService
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(Inventory inventory) {
        mongoTemplate.save(inventory);
    }

    @Override
    public List<Inventory> inventoryList(String status) {
        Query query = new Query(Criteria.where("status").is(status));
        List<Inventory> list = mongoTemplate.find(query, Inventory.class);
        return list;
    }

    @Override
    public List<Inventory> inventoryPage(InventoryQuery inventoryQuery) {
        Query query = new Query(Criteria.where("status").is(inventoryQuery.getStatus()));
        query.with(Sort.by(Sort.Order.desc("qty")));
        query.skip((long)(inventoryQuery.getPage()-1)*inventoryQuery.getPageSize());
        query.limit(inventoryQuery.getPageSize());
        return mongoTemplate.find(query, Inventory.class);
    }
}
